<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Api_V2_Coupon extends Controller_Pc_Api_v2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Coupon
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Coupon
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('coupon', $this->request_params);

        // 服务
        $this->service = new Service_Api_Coupon();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 领券中心 需要登录的接口
    public function action_receive_list()
    {
        try {
            $this->validation->receive_list()->check();

            $this->result['data'] = $this->service->receive_list($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 领券中心 无需登录的接口
    public function action_receive_coupon_list()
    {
        try {
            $this->validation->receive_list()->check();

            $this->result['data'] = $this->service->receive_list($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取优惠券
    public function action_receive()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->receive()->check();

            $this->result = $this->service->receive($this->mid, $this->request_params['id']);

            $db->commit();
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();

            $db->rollback();
        }
    }


    // 会员优惠券
    public function action_member_coupon()
    {
        try {
            $this->validation->member_coupon()->check();

            $this->result['data'] = $this->service->member_coupon($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 会员积分兑换的优惠券
    public function action_member_coupon_jifen() {
        try {
            $this->validation->member_coupon_jifen()->check();

            $this->result['data'] = $this->service->member_coupon_jifen($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 产品可用优惠券
    public function action_product_use_list()
    {
        try {
            $this->validation->product_use_list()->check();

            $this->result['data'] = $this->service->product_use_list($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getTrace();
        }
    }

    // 获得优惠券类别
    public function action_get_coupon_category()
    {
        try {
            $this->validation->get_coupon_category()->check();

            $this->result['data'] = $this->service->get_coupon_category($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 检查优惠券金额
    public function action_check_samount()
    {
        try {
            $this->validation->check_samount()->check();

            $this->result['data'] = $this->service->check_samount($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }


    // 优惠券可使用的产品
    public function action_use_product()
    {
        try {
            $this->validation->use_product()->check();

            $this->result['data'] = $this->service->use_product($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    // 获取产品可用优惠券
    public function action_get_coupon_list()
    {
        try {
            $this->validation->get_coupon_list()->check();

            $this->result['data'] = $this->service->get_coupon_list($this->request_params, $this->mid);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }
}